// 日志数据
let logs = JSON.parse(localStorage.getItem('logs')) || [];

// 获取用户IP地址
async function getIPAddress() {
    try {
        const response = await fetch('https://api.ipify.org?format=json');
        const data = await response.json();
        return data.ip;
    } catch (error) {
        console.error('获取IP地址失败:', error);
        return '未知IP';
    }
}

// 记录日志
async function logSearch(engine, query) {
    const ip = await getIPAddress();
    const timestamp = new Date().toISOString();
    const logEntry = `[${timestamp}] [${ip}] [${engine}] 搜索内容: ${query}\n`;
    logs.push(logEntry); // 将日志添加到数组中
    localStorage.setItem('logs', JSON.stringify(logs)); // 保存到 localStorage
    console.log('日志已记录:', logEntry.trim());
}



// 下载日志
document.getElementById('download-log').addEventListener('click', () => {
    const logContent = logs.join(''); // 将日志数组转换为字符串
    const blob = new Blob([logContent], { type: 'text/plain' }); // 创建Blob对象
    const url = URL.createObjectURL(blob); // 生成下载链接

    // 创建下载链接
    const a = document.createElement('a');
    a.href = url;
    a.download = 'log.txt'; // 下载文件名
    a.click(); // 触发下载

    URL.revokeObjectURL(url); // 释放URL对象
});

// 清空日志（可选）
function clearLogs() {
    logs = [];
    localStorage.removeItem('logs');
    console.log('日志已清空');
}
// 清空日志
function clearLogs() {
    logs = []; // 清空内存中的日志数组
    localStorage.removeItem('logs'); // 删除 localStorage 中的日志数据
    console.log('日志已清空');
    alert('日志已清空');
}
// 添加清空日志按钮
const clearLogButton = document.createElement('button');
clearLogButton.textContent = '清空日志';
clearLogButton.id = 'clear-log';
clearLogButton.style.position = 'fixed';
clearLogButton.style.bottom = '60px'; // 放在下载日志按钮上方
clearLogButton.style.right = '20px';
clearLogButton.style.padding = '10px 20px';
clearLogButton.style.fontSize = '16px';
clearLogButton.style.fontWeight = 'bold';
clearLogButton.style.border = 'none';
clearLogButton.style.borderRadius = '5px';
clearLogButton.style.backgroundColor = '#dc3545';
clearLogButton.style.color = 'white';
clearLogButton.style.cursor = 'pointer';
clearLogButton.style.transition = 'background-color 0.2s ease';

clearLogButton.addEventListener('click', clearLogs);
document.body.appendChild(clearLogButton);


